package com.ohos.connect.devicecloud.iot.modular.pro.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.ohos.connect.devicecloud.common.pojo.CommonResult;
import com.ohos.connect.devicecloud.iot.modular.pro.result.BizDpProductDetailResult;
import com.ohos.connect.devicecloud.iot.modular.pro.service.ProductService;
import com.ohos.connect.devicecloud.iot.modular.pro.utils.RestTemplateHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author MZQ
 * @Description
 * @Date 2025/6/4 11:15
 **/
@Service
public class ProductServiceImpl implements ProductService {


    @Resource
    RestTemplateHelper restTemplateHelper;

    @Override
    public BizDpProductDetailResult getByProdId(String prodId) {
        CommonResult commonResult = restTemplateHelper.getProductDetail(prodId);
        String  json = JSON.toJSONString(commonResult.getData());
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        BizDpProductDetailResult bizDpProductDetailResult = JSON.parseObject(json, BizDpProductDetailResult.class);

        return bizDpProductDetailResult;
    }

    @Override
    public String downloadProfile(String prodId) {
        BizDpProductDetailResult bizDpProductDetailResult = getByProdId(prodId);
        if (ObjectUtil.isEmpty(bizDpProductDetailResult) || StringUtils.isEmpty(bizDpProductDetailResult.getProFileId())) {
            return StringUtils.EMPTY;
        }
        CommonResult commonResult = restTemplateHelper.getProFile(bizDpProductDetailResult.getProFileId());
        String json = commonResult.getData().toString();
        if (StringUtils.isEmpty(json)) {
            return StringUtils.EMPTY;
        }
        return json;
    }
}
